using HKB.Common;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace HKB.Web
{
	public class Shop_DeliverItem
	{
		private Conn conn = null;
		public Shop_DeliverItem(Conn _conn)
		{
			this.conn = _conn;
		}
		public Shop_DeliverItem()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public DataTable GetDeliverItemByOrderFormID(int OrderFormID)
		{
			return this.conn.GetDataTable("SELECT * From [HLShop_DeliverItem] Where OrderID=" + OrderFormID);
		}
		public bool DeleteDeliverItem(string ArrayItemID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ArrayItemID", DbType.String, ArrayItemID);
			return this.conn.Execute("DELETE FROM [HLShop_DeliverItem] WHERE  [DeliverID] in(SELECT * FROM fn_Split(@ArrayItemID,','))", new IDbDataParameter[]
			{
				dbDataParameter
			}) > 0;
		}
		public DataTable GetDeliverByUserNameInputerBegindDateEndDateType(int InputerID, string HanderName, string UserName, string BeginDate, string EndDate, int DeliverType)
		{
			SqlParameter[] array = new SqlParameter[6];
			array[0] = new SqlParameter("@InputerID", DbType.Int32);
			array[0].Value = InputerID;
			array[1] = new SqlParameter("@HandlerName", DbType.String);
			array[1].Value = HanderName;
			array[2] = new SqlParameter("@UserName", DbType.String);
			array[2].Value = UserName;
			array[3] = new SqlParameter("@BeginDate", DbType.String);
			array[3].Value = BeginDate;
			array[4] = new SqlParameter("@EndDate", DbType.String);
			array[4].Value = EndDate;
			array[5] = new SqlParameter("@DeliverType", DbType.Int32);
			array[5].Value = DeliverType;
			return this.conn.GetDataTable("Pr_GetDeliverByUserNameInputerBegindDateEndDateType", array);
		}
		public DataTable GetDeliverItemByItemID(int DeliverID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@DeliverID", DbType.Int32, DeliverID);
			return this.conn.GetDataTable("SELECT * FROM HLShop_DeliverItem WHERE (DeliverID = @DeliverID)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public bool AddDeliverItem(int UserID, int InputerID, int OrderID, int DeliverType, string DeliverName, DateTime DeliverDate, string ExpressNumber, string DeliverCompany, string HandlerName, string Remark)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Inputer", DbType.Int32, InputerID),
				this.conn.CreateParameter("@OrderID", DbType.Int32, OrderID),
				this.conn.CreateParameter("@DeliverType", DbType.Int32, DeliverType),
				this.conn.CreateParameter("@DeliverDate", DbType.DateTime, DeliverDate),
				this.conn.CreateParameter("@ExpressNumber", DbType.String, ExpressNumber),
				this.conn.CreateParameter("@DeliverCompany", DbType.String, DeliverCompany),
				this.conn.CreateParameter("@HandlerName", DbType.String, HandlerName),
				this.conn.CreateParameter("@Remark", DbType.String, Remark),
				this.conn.CreateParameter("@Received", DbType.Boolean, false),
				this.conn.CreateParameter("@DeliverName", DbType.String, DeliverName)
			};
			int num = this.conn.Execute("INSERT INTO [HLShop_DeliverItem] ([UserID], [OrderID], [DeliverDate], [DeliverType], [DeliverName], [HandlerName], [DeliverCompany], [ExpressNumber], [Remark], [Received], [Inputer]) VALUES (@UserID, @OrderID, @DeliverDate, @DeliverType, @DeliverName, @HandlerName, @DeliverCompany, @ExpressNumber, @Remark, @Received, @Inputer)", parameters);
			return num == 1;
		}
		public bool AddDeliverItemTransaction(int UserID, int InputerID, int OrderID, int DeliverType, string DeliverName, DateTime DeliverDate, string ExpressNumber, string DeliverCompany, string HandlerName, string Remark, string r_UserName, string r_Province, string r_City, string r_Area, string r_Address, string r_ZipCode, string r_TelPhone, string r_MobilePhone, string r_Email, string r_OwnerUser)
		{
			bool result = false;
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
				this.conn.CreateParameter("@Inputer", DbType.Int32, InputerID),
				this.conn.CreateParameter("@OrderID", DbType.Int32, OrderID),
				this.conn.CreateParameter("@DeliverType", DbType.Int32, DeliverType),
				this.conn.CreateParameter("@DeliverDate", DbType.DateTime, DeliverDate),
				this.conn.CreateParameter("@ExpressNumber", DbType.String, ExpressNumber),
				this.conn.CreateParameter("@DeliverCompany", DbType.String, DeliverCompany),
				this.conn.CreateParameter("@HandlerName", DbType.String, HandlerName),
				this.conn.CreateParameter("@Remark", DbType.String, Remark),
				this.conn.CreateParameter("@Received", DbType.Boolean, false),
				this.conn.CreateParameter("@DeliverName", DbType.String, DeliverName)
			};
			string commandText = "INSERT INTO [HLShop_DeliverItem] ([UserID], [OrderID], [DeliverDate], [DeliverType], [DeliverName], [HandlerName], [DeliverCompany], [ExpressNumber], [Remark], [Received], [Inputer]) VALUES (@UserID, @OrderID, @DeliverDate, @DeliverType, @DeliverName, @HandlerName, @DeliverCompany, @ExpressNumber, @Remark, @Received, @Inputer)  select SCOPE_IDENTITY()";
			string commandText2 = "INSERT INTO [HLShop_AddressToDeliver] ([DeliverItemID],[UserID],[UserName],[Province],[City],[Area],[Address],[ZipCode],[TelPhone],[MobilePhone],[Email],[OwnerUser]) VALUES (@DeliverItemID,@UserID,@UserName,@Province,@City,@Area,@Address,@ZipCode,@TelPhone,@MobilePhone,@Email,@OwnerUser)";
			string commandText3 = "update HLShop_Order set DeliverStatus=-1 where OrderFormID=" + OrderID;
			this.conn.BeginTransaction();
			try
			{
				object obj = this.conn.ExecuteTransactionScalar(commandText, parameters);
				if (obj != null)
				{
					int num = Convert.ToInt32(obj);
					if (DeliverType == 1)
					{
						this.conn.ExecuteTransaction(commandText3);
					}
					IDbDataParameter[] parameters2 = new IDbDataParameter[]
					{
						this.conn.CreateParameter("@DeliverItemID", DbType.Int32, num),
						this.conn.CreateParameter("@UserID", DbType.Int32, UserID),
						this.conn.CreateParameter("@UserName", DbType.String, r_UserName),
						this.conn.CreateParameter("@Province", DbType.String, r_Province),
						this.conn.CreateParameter("@City", DbType.String, r_City),
						this.conn.CreateParameter("@Area", DbType.String, r_Area),
						this.conn.CreateParameter("@Address", DbType.String, r_Address),
						this.conn.CreateParameter("@ZipCode", DbType.String, r_ZipCode),
						this.conn.CreateParameter("@TelPhone", DbType.String, r_TelPhone),
						this.conn.CreateParameter("@MobilePhone", DbType.String, r_MobilePhone),
						this.conn.CreateParameter("@Email", DbType.String, r_Email),
						this.conn.CreateParameter("@OwnerUser", DbType.String, r_OwnerUser)
					};
					this.conn.ExecuteTransaction(commandText2, parameters2);
				}
			}
			catch
			{
				this.conn.Rollback();
				result = false;
			}
			finally
			{
				this.conn.Commit();
				result = true;
			}
			return result;
		}
		public bool UpdateReceivedStatus(int OrderFormID)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@OrderID", DbType.Int32, OrderFormID);
			return this.conn.Execute("UPDATE [HLShop_DeliverItem] SET  [Received] = 1 WHERE  [OrderID] = @OrderID and [DeliverType] =0 and [DeliverDate]=(select max([DeliverDate]) from [HLShop_DeliverItem] Where OrderID=@OrderID and [DeliverType]=0)", new IDbDataParameter[]
			{
				dbDataParameter
			}) >= 1;
		}
		public DataTable GetNotReceiveAfter15DaysOrderID()
		{
			return this.conn.GetDataTable("SELECT DeliverID, OrderID,ControlFlag FROM HLShop_DeliverItem WHERE ControlFlag=0 AND DeliverType=0 AND Received=0 AND (DateDiff(d,GetDate(),(DateAdd(d,15,DeliverDate)))<0)");
		}
		public bool UpdateReceivedStatusAfterDays(int OrderID, int das)
		{
			return 1 == this.conn.Execute(string.Concat(new object[]
			{
				"Select count(*) From  [HLShop_DeliverItem] WHERE OrderID=",
				OrderID,
				" AND ControlFlag=0 AND DeliverType=0 AND Received=0 AND (DateDiff(d,DeliverDate,GetDate())>",
				das,
				")"
			}));
		}
	}
}
